package com.party.service.download.impl;

import com.party.common.Page;
import com.party.mapper.DownloadMapper;
import com.party.model.Download;
import com.party.model.DownloadExample;
import com.party.service.download.DownloadService;
import com.party.service.download.DownloadService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class DownloadServiceImpl implements DownloadService {

    @Resource
    DownloadMapper downloadMapper;

    @Override
    public int addDownload(Download download) {
        int num = downloadMapper.insertSelective(download);
        return num;
    }

    @Override
    public int deleteDownloadByPrimaryKey(Integer id) {
        int num = downloadMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += downloadMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public Download getDownloadByPrimaryKey(Integer id) {
        Download download = downloadMapper.selectByPrimaryKey(id);
        return download;
    }

    @Override
    public int updateDownloadByPrimaryKey(Download download) {
        int num = downloadMapper.updateByPrimaryKeySelective(download);
        return num;
    }

    @Override
    public Page getDownloadList(Download download, int pageSize, int pageNumber) {
        DownloadExample downloadExample = new DownloadExample();
        /**
         * 可以添加查询条件
         */
        if(download != null)
        {
            DownloadExample.Criteria criteria = downloadExample.createCriteria();
            if(download.getTitle() != null && !"".equals(download.getTitle()))
            {
                criteria.andTitleLike("%"+download.getTitle()+"%");
            }

            if(download.getType() != null && !"".equals(download.getType()))
            {
                criteria.andTypeLike("%"+download.getType()+"%");
            }
        }
        downloadExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = downloadMapper.countByExample(downloadExample);
        //获取指定的页码数据
        downloadExample.setBeginNum((pageNumber - 1) * pageSize);
        downloadExample.setEndNum(pageSize);
        List<Download> list = downloadMapper.getListPage(downloadExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
